home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / findfile / findfile.frm < prev    next >
Text File  |  1995-09-06  |  6KB  |  252 lines

  1. VERSION 2.00
  2. Begin Form FindFile 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Find File"
  5.    ClientHeight    =   2520
  6.    ClientLeft      =   1095
  7.    ClientTop       =   1590
  8.    ClientWidth     =   7365
  9.    Height          =   2925
  10.    Icon            =   FINDFILE.FRX:0000
  11.    Left            =   1035
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   2520
  14.    ScaleWidth      =   7365
  15.    Top             =   1245
  16.    Width           =   7485
  17.    Begin CommandButton CancelBtn 
  18.       Caption         =   "Cancel"
  19.       Height          =   510
  20.       Left            =   5850
  21.       TabIndex        =   5
  22.       Top             =   1710
  23.       Visible         =   0   'False
  24.       Width           =   1230
  25.    End
  26.    Begin CommandButton OKBtn 
  27.       Caption         =   "OK"
  28.       Height          =   510
  29.       Left            =   5850
  30.       TabIndex        =   4
  31.       Top             =   1125
  32.       Width           =   1230
  33.    End
  34.    Begin TextBox Containing 
  35.       Height          =   285
  36.       Left            =   4095
  37.       TabIndex        =   1
  38.       Top             =   675
  39.       Width           =   2985
  40.    End
  41.    Begin PictureBox IncludeSub 
  42.       Height          =   285
  43.       Left            =   2925
  44.       ScaleHeight     =   255
  45.       ScaleWidth      =   2190
  46.       TabIndex        =   2
  47.       Top             =   1260
  48.       Width           =   2220
  49.    End
  50.    Begin TextBox FileSpec 
  51.       Height          =   285
  52.       Left            =   4635
  53.       MaxLength       =   12
  54.       TabIndex        =   0
  55.       Text            =   "*.*"
  56.       Top             =   180
  57.       Width           =   2445
  58.    End
  59.    Begin DirListBox Dir1 
  60.       Height          =   2055
  61.       Left            =   315
  62.       TabIndex        =   6
  63.       Top             =   135
  64.       Width           =   2310
  65.    End
  66.    Begin DriveListBox Drive1 
  67.       Height          =   315
  68.       Left            =   3015
  69.       TabIndex        =   3
  70.       Top             =   1845
  71.       Width           =   2355
  72.    End
  73.    Begin Label Label1 
  74.       BackStyle       =   0  'Transparent
  75.       Caption         =   "Containing:"
  76.       Height          =   240
  77.       Index           =   1
  78.       Left            =   2970
  79.       TabIndex        =   8
  80.       Top             =   720
  81.       Width           =   1095
  82.    End
  83.    Begin Label Label1 
  84.       BackStyle       =   0  'Transparent
  85.       Caption         =   "File Specification:"
  86.       Height          =   240
  87.       Index           =   0
  88.       Left            =   2970
  89.       TabIndex        =   7
  90.       Top             =   225
  91.       Width           =   1770
  92.    End
  93. End
  94.  
  95. Option Explicit
  96. Option Compare Text
  97.  
  98. Dim F1 As Found
  99. Dim CancelFlag As Integer
  100.  
  101. Sub CancelBtn_Click ()
  102.  
  103. CancelFlag = True
  104.  
  105. End Sub
  106.  
  107. Sub Drive1_Change ()
  108.  
  109. Dir1.Path = Left$(Drive1.Drive, 2)
  110.  
  111. End Sub
  112.  
  113. Function FileContains (FileName As String, SearchText As String) As Integer
  114. Dim FileNumber As Integer
  115. Dim FileLength As Long
  116. Dim Chunk As String
  117. Dim ChunkStart As Long
  118. Const MaxChunk = 20000
  119.  
  120. On Error GoTo FileContainsError
  121.  
  122. FileNumber = FreeFile
  123.  
  124. Open FileName For Binary Access Read Shared As FileNumber
  125. FileLength = LOF(FileNumber)
  126. ChunkStart = 0
  127.  
  128. Do Until ChunkStart = FileLength
  129.     If FileLength - ChunkStart > MaxChunk Then
  130.         Chunk = Input$(MaxChunk, FileNumber)
  131.         ChunkStart = ChunkStart + MaxChunk - Len(SearchText)
  132.     Else
  133.         Chunk = Input$(FileLength - ChunkStart, FileNumber)
  134.         ChunkStart = FileLength
  135.     End If
  136.     If InStr(Chunk, SearchText) > 0 Then
  137.         FileContains = True
  138.         Exit Do
  139.     End If
  140. Loop
  141.  
  142. Close FileNumber
  143.  
  144. Exit Function
  145.  
  146. FileContainsError:
  147.     Select Case Err
  148.         Case Else
  149.             MsgBox Error$ & " on file " & FileName
  150.     End Select
  151.     Exit Function
  152.  
  153. End Function
  154.  
  155. Sub Find (SearchPath As String)
  156. ReDim DirName(0 To 15) As String
  157. Dim DirCount As Integer
  158. Dim FileName As String, Attributes As Integer
  159. Dim x As Integer
  160.  
  161. If Right$(SearchPath, 1) <> "\" Then SearchPath = SearchPath & "\"
  162. DirCount = 0
  163. FileName = Dir$(SearchPath & FileSpec, Attr_Normal + Attr_System + Attr_Hidden)
  164. Do Until FileName = ""
  165.     If Containing = "" Then
  166.         F1.FoundFiles.AddItem SearchPath & FileName
  167.     Else
  168.         If FileContains(SearchPath & FileName, (Containing.Text)) Then
  169.             F1.FoundFiles.AddItem SearchPath & FileName
  170.         End If
  171.     End If
  172.     FileName = Dir$
  173.     DoEvents
  174.     If CancelFlag Then Exit Sub
  175. Loop
  176.  
  177. If IncludeSub Then
  178.     FileName = Dir$(SearchPath & "*.*", Attr_Normal + Attr_System + Attr_Hidden + Attr_Directory)
  179.     Do Until FileName = ""
  180.         If FileName <> "." And FileName <> ".." Then
  181.             Attributes = GetAttr(SearchPath & FileName)
  182.             If (Attributes And Attr_Directory) Then
  183.                 If DirCount > UBound(DirName) Then
  184.                     ReDim Preserve DirName(0 To DirCount + 15)
  185.                 End If
  186.                 DirName(DirCount) = SearchPath & FileName
  187.                 DirCount = DirCount + 1
  188.             End If
  189.         End If
  190.         FileName = Dir$
  191.         DoEvents
  192.         If CancelFlag Then Exit Sub
  193.     Loop
  194.     For x = 0 To DirCount - 1
  195.         Find DirName(x)
  196.     Next x
  197. End If
  198.  
  199. End Sub
  200.  
  201. Sub Form_Unload (Cancel As Integer)
  202.  
  203. If Forms.Count > 1 Then
  204.     Select Case MsgBox("Close search windows also?", MB_YesNoCancel)
  205.         Case IDYes
  206.             End
  207.         Case IDCancel
  208.             Cancel = True
  209.     End Select
  210. End If
  211.             
  212. End Sub
  213.  
  214. Sub OKBtn_Click ()
  215.  
  216. 'MousePointer = Hourglass
  217. OKBtn.Enabled = False
  218.  
  219. Caption = "Find File - Searching"
  220. CancelBtn.Visible = True
  221.  
  222. Set F1 = New Found
  223. CancelFlag = False
  224.  
  225. If FileSpec = "" Then FileSpec = "*.*"
  226. Find (Dir1.Path)
  227.  
  228. Caption = "Find File"
  229. CancelBtn.Visible = False
  230.  
  231. If CancelFlag Then
  232.     Unload F1
  233. Else
  234.     Select Case F1.FoundFiles.ListCount
  235.         Case 0
  236.             MsgBox "No files matching the search criteria were found."
  237.             Unload F1
  238.         Case 1
  239.             F1.Caption = F1.FoundFiles.ListCount & " File Found"
  240.             F1.Show
  241.         Case Else
  242.             F1.Caption = F1.FoundFiles.ListCount & " Files Found"
  243.             F1.Show
  244.     End Select
  245. End If
  246.  
  247. OKBtn.Enabled = True
  248. 'MousePointer = Default
  249.  
  250. End Sub
  251.  
  252.